<template>
	<view>
		<!-- 会话页面 -->
		<uni-list>
			<!-- <uni-list-item v-for="item in chatlist" @click="entrychat(item)" 
			clickable :title="item.name">
			
			</uni-list-item> -->

			<uni-list-chat title="uni-app" v-for="item in chatlist" @click="entrychat(item)" :title="item.name"
				clickable avatar="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/unicloudlogo.png"
				:note="item.lastTxt!=null?item.lastTxt:''" :time="timestamp2DateTime(item.lastDate)"
				badge-positon="left" :badge-text="item.noread>=1?item.noread:''"></uni-list-chat>
		</uni-list>

	</view>
</template>

<script>
	import {
		queryChatList,
		saveChatList,
		saveChatDirect,
		updateChatDirect,
		timestamp2DateTime,
		updateChatDirectMsg,
		msg2ChatUser,
	} from "../../utils/utils"
	export default {
		name: "msg",
		data() {
			return {
				chatlist: [],
			}
		},
		methods: {

			timestamp2DateTime,

			entrychat(item) {
				item.groupName = item.name;
				item.username = item.name;
				item.groupId = item.receiverId;
				item.userId = item.receiverId;
				saveChatList(item, item.type);

				if (item.type == 2) {
					uni.navigateTo({
						url: "/pages/chatGroup/chatGroup?groupInfo=" + JSON.stringify(item)
					})
				} else {
					uni.navigateTo({
						url: "/pages/chat/chat?userInfo=" + JSON.stringify(item)
					})
				}


			},

			queryChat(callback) {
				let that = this;
				return queryChatList().then(data => {
					console.log(data);
					that.chatlist = data;
				})
			},
			listenerMsg() {
				let that = this;
				uni.$on("onSocketMessage", (res) => {
					//接收到消息
					let data = JSON.parse(res.data);
					console.log("消息页面消息",data);
					let type = data.type;
					let chatUsers = null;
					if (data.type == 2) {
						chatUsers = that.chatlist.filter(item => item.receiverId == data.groupId && item.type ==
							data.type);
					} else {
						chatUsers = that.chatlist.filter(item => item.receiverId == data.senderId && item
							.type ==
							data.type);
					}
					let perop = null;
					console.log("会话信息", chatUsers,that.chatlist);
					if (chatUsers.length > 0) {
						let chatUser = chatUsers[0];
						let page = getCurrentPages();
						if (page.length > 1) {
							//不在首页，只更新最后消息
							perop = updateChatDirectMsg(chatUser, data.msgContent);
						} else {
							//在首页，修改未读数量和消息
							perop = updateChatDirect(chatUser, data.msgContent);
						}
					} else {
						//临时保存
						let record = msg2ChatUser(data);
						that.chatlist.push(record);
						// //保存会话信息
						perop = saveChatDirect(record);
						//保存
					}
					//更新完数据库的数据，重新查询下数据	
					// perop.then(res => {
					// 	that.queryChat();
					// })
				})
			}

		},
		onReady() {

			//接收到消息
			let userId = uni.getStorageSync("login:userid");
			let that = this;
			this.queryChat().then(() => {
				//会话列表初始化完毕后，监听消息
				that.listenerMsg();
			})
			uni.$on("chatlistflush", () => {
				that.queryChat();
			})
		},
		onUnload() {
			uni.$off("onSocketMessage");
		}
	}
</script>

<style>

</style>